package com.tgu.home_action.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.tgu.home_action.dao.AdminMapper;
import com.tgu.home_action.dao.MemoMapper;
import com.tgu.home_action.dao.VolunteerInformationMapper;
import com.tgu.home_action.dao.VolunteerMapper;
import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.MemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemoServiceImpl implements MemoService {
    @Autowired
    private MemoMapper memoMapper;
    @Autowired
    private VolunteerMapper volunteerMapper;
    @Autowired
    private VolunteerInformationMapper volunteerInformationMapper;
    @Autowired
    private AdminMapper adminMapper;
    /**
     * 志愿者添加备忘录
     */
    @Override
    public ResponseData<Object> volunteerAddMemo(Memo memo, Volunteer volunteer) {
//        memo.setId(null);
        memo.setRole("volunteer");
        memo.setVolunteerId(volunteer.getId());
        String name=volunteerInformationMapper.getNameByVolunteerId(volunteer.getId());
        memo.setName(name);
        memoMapper.insert(memo);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 管理员添加备忘录
     */
    @Override
    public ResponseData<Object> adminAddMemo(Memo memo, Admin admin) {
//        memo.setId(null);
        memo.setRole("admin");
        admin=adminMapper.getById(admin.getId());
        memo.setAdminId(admin.getId());
        memo.setName(admin.getName());
        memoMapper.insert(memo);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 查询备忘录
     */
    @Override
    public ResponseData<List<Memo>> getMemo(Memo memo) {
        QueryWrapper<Memo> memoQueryWrapper=new QueryWrapper<>();
        if(memo.getTitle()!=null){
            memoQueryWrapper.like("title","%"+memo.getTitle()+"%");
        }
        if(memo.getId()!=null){
            memoQueryWrapper.eq("id",memo.getId());
        }
        if(memo.getRole()!=null){
            memoQueryWrapper.eq("role",memo.getRole());
        }
        if(memo.getIsGlobal()!=null){
            memoQueryWrapper.eq("is_global",memo.getIsGlobal());
        }
        if(memo.getVolunteerId()!=null){
            memoQueryWrapper.eq("volunteer_id",memo.getVolunteerId());
        }
        if(memo.getAdminId()!=null){
            memoQueryWrapper.eq("admin_id",memo.getAdminId());
        }
        if(memo.getCaseId()!=null){
            if(memo.getIncludeGlobal()!=null&&memo.getIncludeGlobal()==1){
                memoQueryWrapper.eq("case_id",memo.getCaseId()).or().eq("is_global",1);
            }else {
                memoQueryWrapper.eq("case_id",memo.getCaseId());
            }
        }
        memoQueryWrapper.orderByDesc("create_at");
        List<Memo> memos=memoMapper.selectList(memoQueryWrapper);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),memos);
    }

    /**
     * 志愿者修改备忘录
     */
    @Override
    public ResponseData<Object> volunteerUpdateMemo(Memo memo, Volunteer volunteer) {
        Memo oldMemo=memoMapper.selectById(memo.getId());
        if(oldMemo==null){
            return new ResponseData<>(ResponseState.MEMO_NOT_EXIST.getMessage(), ResponseState.MEMO_NOT_EXIST.getValue());
        }
        if(!(oldMemo.getRole().equals("volunteer")&& oldMemo.getVolunteerId().equals(volunteer.getId()))){
            return new ResponseData<>(ResponseState.WITHOUT_PERMISSION.getMessage(), ResponseState.WITHOUT_PERMISSION.getValue());
        }
        memoMapper.updateById(memo);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 管理员修改备忘录
     */
    @Override
    public ResponseData<Object> adminUpdateMemo(Memo memo) {
        Memo oldMemo=memoMapper.selectById(memo.getId());
        if(oldMemo==null){
            return new ResponseData<>(ResponseState.MEMO_NOT_EXIST.getMessage(), ResponseState.MEMO_NOT_EXIST.getValue());
        }
        memoMapper.updateById(memo);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 志愿者删除备忘录
     */
    @Override
    public ResponseData<Object> volunteerDeleteMemo(Integer id, Volunteer volunteer) {
        Memo oldMemo=memoMapper.selectById(id);
        if(oldMemo==null){
            return new ResponseData<>(ResponseState.MEMO_NOT_EXIST.getMessage(), ResponseState.MEMO_NOT_EXIST.getValue());
        }
        System.out.println(volunteer);
        if(!(oldMemo.getRole().equals("volunteer")&&oldMemo.getVolunteerId().equals(volunteer.getId()))){
            return new ResponseData<>(ResponseState.WITHOUT_PERMISSION.getMessage(), ResponseState.WITHOUT_PERMISSION.getValue());
        }
        memoMapper.deleteById(id);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }

    /**
     * 管理员删除备忘录
     */
    @Override
    public ResponseData<Object> adminDeleteMemo(Integer id) {
        Memo oldMemo=memoMapper.selectById(id);
        if(oldMemo==null){
            return new ResponseData<>(ResponseState.MEMO_NOT_EXIST.getMessage(), ResponseState.MEMO_NOT_EXIST.getValue());
        }
        memoMapper.deleteById(id);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
    }
}
